﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Bitmap bitmap;
        public Form1()
        {
            InitializeComponent();
           // bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            DrawHeat();

        }

        Point pushPoint = new Point(0, 0);
        List<LineInfo> lineList = new List<LineInfo>();
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!pushPoint.Equals(new Point(0, 0)))
                {
                    lineList.Add(new LineInfo()
                    {
                        angle = GetAngle(pushPoint, e.Location),
                        start_point = pushPoint,
                        end_point = e.Location,
                        length = GetLength(pushPoint, e.Location),
                        start_position = 0,
                        points = GetScale(pushPoint, e.Location)
                    });
                }
                pushPoint = e.Location;
            }
            if (e.Button == MouseButtons.Right)
            {
                pushPoint = new Point(0, 0);
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (lineList.Count == 0 && pushPoint.Equals(new Point(0, 0))) return;
            DrawLine(0, pushPoint, e.Location);
        }

        void DrawLine(double startLength, Point startPoint, Point endPoint)
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.Clear(Color.White);

                foreach (var v in lineList)
                {
                    g.ResetTransform();
                    g.DrawLine(new Pen(Brushes.Black, 1), v.start_point, v.end_point);
                    DrawScale(v.start_point, v.end_point, g);
                }
                g.ResetTransform();
                g.DrawLine(new Pen(Brushes.Black, 1), startPoint, endPoint);
                DrawScale(startPoint, endPoint, g);

                pictureBox1.Image = bitmap;
            }
        }



        void DrawScale(Point startPoint, Point endPoint, Graphics g)
        {
            double angleOfLine = GetAngle(startPoint, endPoint);

            var list = GetScale(startPoint, endPoint);

            var x = g.Transform;
            for (int i = 0; i < list.Count; i++)
            {
                g.ResetTransform();
                g.TranslateTransform(list[i].X, list[i].Y);
                g.RotateTransform((float)angleOfLine - 180);
                g.DrawLine(new Pen(Brushes.Black, 1), 0, i % 10 == 0 ? -10 : (i % 2 == 0 ? -10 : 0), 0, i % 10 == 0 ? 10 : (i % 2 == 1 ? 10 : 0));
            }
        }
        double GetLength(Point startPoint, Point endPoint)
        {
            return Math.Pow(Math.Pow(endPoint.Y - startPoint.Y, 2) + Math.Pow(endPoint.X - startPoint.X, 2), 0.5);
        }

        double GetAngle(Point startPoint, Point endPoint)
        {
            return Math.Atan2(((double)endPoint.Y - (double)startPoint.Y), ((double)endPoint.X - (double)startPoint.X)) * 180.0 / Math.PI;
        }
        List<Point> GetScale(Point startPoint, Point endPoint)
        {
            List<Point> points = new List<Point>();
            int step = 10;
            double length = GetLength(startPoint, endPoint);
            double scale = step;
            double x, y;
            while (scale < length)
            {
                x = startPoint.X + scale / length * (endPoint.X - startPoint.X);
                y = startPoint.Y + (scale / length) * (endPoint.Y - startPoint.Y);

                points.Add(new Point((int)x, (int)y));

                scale += step;
            }
            return points;
        }



        public void DrawHeat()
        {
            string ParamPalettePath = $"{System.Environment.CurrentDirectory}/ttt.png";//颜色板

            List<double> lon = new List<double>();
            List<double> lat = new List<double>();
            List<double> weight = new List<double>();
            //DataTable dt = GetExcelTable(filePath, sheetName, 2);
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    string[] lonAndLat = dt.Rows[i]["坐标值"].ToString().Split(',');
            //    double lonStr = Convert.ToDouble(lonAndLat[0]);
            //    double latStr = Convert.ToDouble(lonAndLat[1]);
            //    double weightStr = Convert.ToDouble(dt.Rows[i]["权重值"].ToString());
            //    lon.Add(lonStr); lat.Add(latStr); weight.Add(weightStr);
            //}
            Random ran = new Random();
            for (int i = 0; i < 300; i++)
            {
                lon.Add(ran.Next(0,90)); lat.Add(ran.Next(0,90)); weight.Add(ran.Next(20));
            }

            double latRange = Math.Abs(lat.Max() - lat.Min());
            double lngRange = Math.Abs(lon.Max() - lon.Min());
            int width = 0;
            int height = 0;
            double scale = 1;
            if (lngRange > latRange)
            {
                width = 500;
                scale = lngRange / 500;
                height = (int)(latRange / lngRange * 500);
            }
            else
            {
                height = 500;
                scale = latRange / 500;
                width = (int)(lngRange / latRange * 500);
            }
            Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            RayHeatMap heatMap = new RayHeatMap();
            heatMap.BrushStop = 0.7f;
            heatMap.Radius = 30;
            for (int i = 0; i < lat.Count; i++)
            {
                int y = (int)((lat[i] - lat.Min()) / scale);
                int x = (int)((lon[i] - lon.Min()) / scale);
                int weightInt = (int)((double)weight[i] / weight.Max() * 255);
                heatMap.AddPoint(new Point(x, y), weightInt);
            }
            heatMap.Draw(ref bitmap);
            heatMap.Colorize(ref bitmap, ParamPalettePath);


            pictureBox1.Image = bitmap;
            //DirectoryInfo dir = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ParamPalettePath), "ShenZhen"));
            //if (!dir.Exists)
            //    dir.Create();
            //string name = DateTime.Now.ToString("yyyyMMddHHmm") + ".png";
            //string filePaths = System.IO.Path.Combine(dir.ToString(), name);
            //FileStream fs = File.Open(filePaths, FileMode.Create);
            //bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
            //fs.Close();
        }



    }

    public class LineInfo
    {
        public Point start_point { get; set; }
        public Point end_point { get; set; }
        public double length { get; set; }
        public List<Point> points { get; set; }
        public double angle { get; set; }
        public double start_position { get; set; } = 0;
    }
}